// 导入express
const express=require('express')
//创建express服务器实例
const app=express()
//导入cors中间件
const cors=require('cors')
//导入joi
const joi =require('joi')
const path=require('path')
//注册为全局中间件
app.use(cors())
//解析application/x-www-form-urlencoded格式的表单数据的中间件
app.use(express.urlencoded({extended:false}))

//响应数据中间件
app.use((req,res,next)=>{
    res.cc=function(err,status = 1){

        res.send(
            {
                status,
                message:err instanceof Error ? err.message : err,
            }
        )
    }
    next()
})
app.use('/css',express.static(path.join(__dirname, 'public/css')));
app.use('/fonts',express.static(path.join(__dirname, 'public/fonts')));
//app.use('/',express.static(path.join(__dirname, 'public/html')));
app.use('/img',express.static(path.join(__dirname, 'public/img')));
app.use('/js',express.static(path.join(__dirname, 'public/js')));


app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/login.html'));
});
app.get('/login', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/login.html'));
});
app.get('/cust', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/cust.html'));
});
app.get('/doc', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/doc.html'));
});
app.get('/home', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/home.html'));
});
app.get('/med', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/med.html'));
});
app.get('/oper', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/oper.html'));
});
app.get('/purmed', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/purmed.html'));
});
app.get('/supmed', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/supmed.html'));
});
app.get('/sort', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/sort.html'));
});
app.get('/sup', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/html/sup.html'));
});
//路由前配置路由拦截api可访问
const {expressjwt:jwt }= require('express-jwt')
const config = require('./config')
app.use(jwt({secret: config.jwtSecretKey ,algorithms:["HS256"]}).unless({ path: [/^\/api/]}))


// 导入并使用用户路由模块
const loginRouter = require('./router/login')
app.use('/api',loginRouter)

const customerRouter=require('./router/customer')
app.use('/customer',customerRouter)

const doctorRouter=require('./router/doctor')
app.use('/doctor',doctorRouter)

const operatorRouter=require('./router/operator')
app.use('/operator',operatorRouter)

const supplierRouter=require('./router/supplier')
app.use('/supplier',supplierRouter)

const medicineRouter=require('./router/medicine')
app.use('/medicine',medicineRouter)

const sortRouter=require('./router/sort')
app.use('/medsort',sortRouter)

const med_purRouter=require('./router/med_pur')
app.use('/medpur',med_purRouter)

const med_supRouter=require('./router/med_sup')
app.use('/medsup',med_supRouter)



//注册全局错误级别中间件
app.use((err,req,res,next)=>{
    if(err instanceof joi.ValidationError) return res.cc(err)
    // 捕获身份认证失败的错误
    if (err.name === 'UnauthorizedError') return res.cc('identify error!')
    res.cc(err)
})

//调用app.listen指定端口号启动服务器
app.listen(8000,()=>{
    console.log('server running at http://127.0.0.1:8000')
})